package com.itheima.dao;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import javax.annotation.Resources;
import javax.swing.*;
import java.util.List;

/**
 * 在mybatis中针对CRUDE一共有四个注解
 * @SSelect  @Insert  @Update #Delete
 */
@CacheNamespace(blocking =  true)
public interface IUser {

    /**
     * 查询所有用户
     * @return
     */
    @Select(value = "select * from user" )
    @Results(id = "userMap",value = {
            @Result(id = true,column = "id" ,property = "userId"),
            @Result( column = "username" ,property = "userName"),
            @Result( column = "address" ,property = "userAddress"),
            @Result( column = "sex" ,property = "userSex"),
            @Result( column = "birthday" ,property = "userBirthday"),
            @Result(property = "accounts" ,column = "id",
                    many =@Many(select = "com.itheima.dao.IAccount.findAccountById",
                            fetchType = FetchType.LAZY))})
    List<User> findAll();


    @Select("select * from user where id = #{id}")
    @ResultMap(value = {"userMap"})
    User findById(Integer id);

    /**
     * 根据名称查询用户
     * @param username
     * @return
     */
    @Select("select * from user where username like #{username}")
    @ResultMap(value = {"userMap"})
    List<User> findUserByName(String username);


}
